package hibernate_basic.Persistence.query_data;

import hibernate_basic.Persistence.comment_persitence.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * @author Administrator
 * @create 2018-12-31 9:08
 * @desc 查询数据的两种方法
 **/
public class Query {

    public static void main(String args[]){
       try{
           Configuration cfg=new Configuration();
           cfg.configure("hibernate_basic/config/config_comment/hibernate.cfg.xml");
           SessionFactory factory=cfg.buildSessionFactory();
           Session session=factory.openSession();
           Transaction tran=session.beginTransaction();
//           get方法
           Person person=(Person)session.get(Person.class,new Integer("10"));
           System.out.println(person.getId());
           System.out.println(person.getName());
           System.out.println(person.getAge());
           System.out.println(person.getHeight());
           System.out.println(person.getWeight());

//           load方法
           Person person1=session.load(Person.class,10);
           System.out.println(person1.getId());
           System.out.println(person1.getName());
           System.out.println(person1.getAge());
           System.out.println(person1.getHeight());
           System.out.println(person1.getWeight());

       }catch(Exception e) {
           e.printStackTrace();
           System.out.println("对象装载失败!");
       }
    }
}
